import mysql2 from 'mysql2/promise';
import dotenv from 'dotenv';
import path from 'path';

// 加载环境变量
dotenv.config({
  path: [
    path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || 'development'}`),
    path.resolve(process.cwd(), '.env'),
  ]
});

async function checkDatabase() {
  const connection = await mysql2.createConnection({
    host: process.env.DB_HOST,
    port: process.env.DB_PORT,
    user: process.env.DB_USER,
    password: process.env.DB_PASSWORD,
    database: process.env.DB_DATABASES,
  });

  try {
    // 查看分类表
    console.log('=== 分类表数据 ===');
    const [categories] = await connection.execute('SELECT * FROM categories LIMIT 10');
    console.table(categories);

    // 查看文章表的分类字段
    console.log('\n=== 文章表分类字段 ===');
    const [articles] = await connection.execute('SELECT id, title, categoryId FROM articles WHERE isDelete = 0 LIMIT 5');
    console.table(articles);

    // 查看文章和分类的关联
    console.log('\n=== 文章分类关联 ===');
    const [joined] = await connection.execute(`
      SELECT a.id, a.title, a.categoryId, c.name as categoryName 
      FROM articles a 
      LEFT JOIN categories c ON a.categoryId = c.id 
      WHERE a.isDelete = 0 
      LIMIT 5
    `);
    console.table(joined);

  } finally {
    await connection.end();
  }
}

checkDatabase().catch(console.error);
